//
//  BaseWebController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/28.
//

#import "BaseWebController.h"
#import <Webkit/WebKit.h>

static void *keyContext = &keyContext;

@interface BaseWebController ()<WKNavigationDelegate,WKUIDelegate>

@property (nonatomic, strong) WKWebView *webview;
@property (nonatomic, strong) UIProgressView *progress;

@end

@implementation BaseWebController

- (instancetype)init {
    if(self  = [super init]) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        self.webview = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
        self.webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.progress removeFromSuperview];
}

- (void)initDidLoad {
    if (self.titleStr.length > 0 ) {
        self.navigationTitle = self.titleStr;
    }
    self.hbd_barAlpha = 1;
}

- (void)initViews {
    self.webview.frame = CGRectMake(0, 0, kWidthForScreen, kHeightForScreen);
    [self.webview setNavigationDelegate:self];
    [self.webview setUIDelegate:self];
    [self.webview.scrollView setAlwaysBounceVertical:YES];
    self.webview.backgroundColor = [UIColor whiteColor];
    self.webview.hidden = YES;
    self.webview.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.webview];
    
    [self.webview addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:keyContext];
    
    self.progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    [self.progress setTrackTintColor:[UIColor whiteColor]];
    [self.progress setProgressTintColor:kBgColor];
    [self.progress setFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height-self.progress.frame.size.height, self.view.frame.size.width, self.progress.frame.size.height)];
    [self.progress setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
    [self.navigationController.navigationBar addSubview:self.progress];
}

- (void)setUrlStr:(NSString *)urlStr {
    _urlStr = urlStr;
    NSURL *URL = [NSURL URLWithString:urlStr];
    [self.webview loadRequest:[NSURLRequest requestWithURL:URL]];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 0.1*NSEC_PER_SEC);
    dispatch_after(time, dispatch_get_main_queue(), ^{
        webView.hidden = NO;
    });
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))]
            && object == _webview) {
            self.progress.progress = _webview.estimatedProgress;
            if (_webview.estimatedProgress >= 1.0f) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.progress.progress = 0;
                });
            }
        }else{
            [super observeValueForKeyPath:keyPath
                                 ofObject:object
                                   change:change
                                  context:context];
        }
}

- (void)dealloc {
    self.webview.UIDelegate = nil;
    self.webview.navigationDelegate = nil;
    [self.webview removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
    if (_webview) {
        [_webview removeFromSuperview];
        _webview = nil;
    }
}

@end
